package org.grill.clovercash;

import java.io.File;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.grill.clovercash.accounts.Account;
import org.grill.clovercash.accounts.Account.DeltaEvent;
import org.grill.clovercash.accounts.Account.IDeltaListener;
import org.grill.clovercash.storage.AccountReader;
import org.grill.clovercash.storage.AccountWriter;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class CloverCashUIPlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.grill.clovercash";

	// The shared instance
	private static CloverCashUIPlugin plugin;
	
	private Account rootAccount;
	
	/**
	 * The constructor
	 */
	public CloverCashUIPlugin() {
		plugin = this;
		
		File file = new File("user.home" + "/Library/Application Support/CloverCash/");
		if (!file.exists())
			file.mkdirs();
		
		final String filename = System.getProperty("user.home") + "/Library/Application Support/CloverCash/data.xml";
		rootAccount = new AccountReader(filename).read();
		if (rootAccount == null) 
			rootAccount = new Account("ROOT");
		
		rootAccount.addListener(new IDeltaListener() {
			volatile boolean currentlyWaiting = false;
			
			public void remove(DeltaEvent event) {
				add(event);
			}
		
			public void add(DeltaEvent event) {
				if (currentlyWaiting)
					return;
				
				new Timer().schedule(new TimerTask() {
					@Override
					public void run() {
						String string = filename + "-" + (Calendar.getInstance().getTimeInMillis()/1000000);
						new AccountWriter(string, rootAccount).write();
						new AccountWriter(filename, rootAccount).write();
//						System.out.println("Wrote " + string);
						
						currentlyWaiting = false;
					}
				}, 50);
			}
		
		});
		
		/**
		Account account = new Account("Foo", "", Type.ASSET, rootAccount);
		
		new Account("Bar", "", Type.ASSET, account);
		
		
		Account other = new Account("Other", "", Type.ASSET, rootAccount);
		
		Transaction t = new Transaction(account, other, "Money Out", new BigDecimal(-10.25), new LocalDate());
		
		new AccountWriter("asdf", rootAccount).write();
		*/
	}
	
	public Account getRootAccount() {
		return rootAccount;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static CloverCashUIPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, "icons/" + path);
	}
	
	
}
